package fms.webservice.server.parser;

import fms.webservice.server.mq.ISender;
import fms.webservice.server.util.ObjectUtil;
import fms.webservice.server.util.ValidCheckAviation;
import fms.webservice.server.util.XmlUtil;
import fms.webservice.server.common.*;
import org.apache.log4j.Logger;

/**
 * Created with IntelliJ IDEA.
 * User: root
 * Date: 14-7-9
 * Time: 上午9:38
 * To change this template use File | Settings | File Templates.
 */
public class PosRequestParser implements IRequestParser {
    private ISender sender;
    private static Logger logger = Logger.getLogger(PosRequestParser.class);

    public void setSender(ISender sender) {
        this.sender = sender;
    }

    public Response parser(fms.webservice.server.common.Request posRequest) {
        Response response = new Response();
        response.setVersion("1.0");
        response.setServiceType( String.valueOf(ServiceType.PosRequest.ordinal()) );
        PosResponseXML posResponseXML = new PosResponseXML();

//        System.out.println("\n\n[Begin]PosRequestParser:parser:\n" + posRequest.getRequestXML());
        PosRequestXML posRequestXML = XmlUtil.unMarshallPosRequest(posRequest.getRequestXML());
//        System.out.println(posRequestXML.getAircraftid() + "\n"  + posRequestXML.getCptname()  + "\n");

        if ( !ObjectUtil.isBlank(posRequestXML.getAircraftid())
                && ValidCheckAviation.RegexCheck(posRequestXML.getCptname(), ValidCheckAviation.CheckType.AcarsFreeText.ordinal()) ) {
//                && ValidCheckAviation.CheckAircraftId(posRequestXML.getAircraftid()) ) {
            posResponseXML.setResultcode(ErrorLevel.Success);
            response.setResponseXML(XmlUtil.marshallPosResponse(posResponseXML));

            // 将位置报请求（组成acars位置报请求报文）发送到消息队列（以便触发poscontroller）
            PosRequestInnerXML posRequestInnerXML = new PosRequestInnerXML();
            if( null == posRequestXML.getAirlines() ) {
                posRequestInnerXML.setAirlines("");
            }
            posRequestInnerXML.setAirlines(posRequestXML.getAirlines());
            posRequestInnerXML.setAircraftid(posRequestXML.getAircraftid());
            posRequestInnerXML.setCptname(posRequestXML.getCptname());
            posRequest.setRequestXML(XmlUtil.marshallInnerRequest(posRequestInnerXML));

//            String reqStr =  XmlUtil.marshallRequest(posRequest);
////            System.out.println( reqStr );
//            logger.info(reqStr);
//            Request req = XmlUtil.unMarshallRequest(reqStr);
//            String reqInnserXML = req.getRequestXML();
//
//            PosRequestInnerXML requestInnerXML = XmlUtil.unMarshallInnerRequest(reqInnserXML );
//            logger.info(requestInnerXML.getAirlines());
//            logger.info(requestInnerXML.getCptname());
////            System.out.println(requestInnerXML.getAirlines());
////            System.out.println(requestInnerXML.getCptname());
//            System.out.println(requestInnerXML.getAircraftid());
//            System.out.println( "---------------\n\n\n" + XmlUtil.marshallRequest(posRequest) );
            // 要确认相应的MQ已经启动了呦，否则程序走到这里会卡死
            sender.send( XmlUtil.marshallRequest(posRequest) );
        } else {
            posResponseXML.setResultcode(ErrorLevel.ParamError);
            logger.error("机尾号、航路点不合法！");
        }
        // 将位置报请求执行状态返回给三方调用者
        return response;
    }
}
